package com.jf.hb;

import com.jf.utils.FileUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-07
 * Time:2:19
 */
public class HB {
    public static File record;
    public static File backupRecord;
    public static String username;
    public static String password;
    public static int luckNum;
    public static String blessingText;
    public static String wishText;

    static {
        //read record and backupRecord
        //InputStream inputStream = HB.class.getClassLoader().getResourceAsStream("conf/config.xml");
        SAXReader saxReader = null;
        //System.out.println(HB.class.getClassLoader().getResource("conf/config.xml"));
        try {
            InputStream inputStream = new FileInputStream("./conf/config.xml");
            saxReader = new SAXReader();
            //Document document = saxReader.read(new File(configUrl));
            Document document = saxReader.read(inputStream);
            if (inputStream != null) {
                inputStream.close();
            }
            Element record = document.getRootElement().element("record");
            String recordURL = record.element("url").getText();
            String backupRecordUrl = record.element("backupUrl").getText();
            HB.record = new File(recordURL);
            HB.backupRecord = new File(backupRecordUrl);
        } catch (DocumentException | IOException e) {
            throw new RuntimeException(e);
        }
        //read username and password
        username = FileUtil.readPropertyValues("conf/login.properties", "username");
        password = FileUtil.readPropertyValues("conf/login.properties", "password");
        luckNum = FileUtil.readXMLLuckyNumber("conf/config.xml");
        setBlessingText();
        readWishText();
    }

    public static void writeUserInformation() {
        FileUtil.writePropertyValues("conf/login.properties", "username", username, "password", password);
    }

    public static void setUserInformation(String u, String p) {
        username = u;
        password = p;
    }

    public static void setBlessingText() {
        int singleDigit = luckNum % 10;
        String singleText = FileUtil.readXMLBlessing("conf/config.xml", singleDigit, "value1");
        int tenDigit = luckNum / 10;
        String tenText = FileUtil.readXMLBlessing("conf/config.xml", tenDigit, "value2");
        String s = "愿你\n青春常驻\n内心如花\n笑容灿烂\n所遇皆暖";
        blessingText = "祝你\n";
        if (tenDigit != 0) blessingText += tenText + "\n";
        blessingText += singleText + "\n";
        blessingText += s + "\n" + getAge() + "岁生日快乐！！！\n";
    }

    private static int getAge() {
        int year = Integer.parseInt(FileUtil.readXMLInformation("conf/config.xml", "year"));
        int month = Integer.parseInt(FileUtil.readXMLInformation("conf/config.xml", "month"));
        int day = Integer.parseInt(FileUtil.readXMLInformation("conf/config.xml", "day"));
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        int curMonth = calendar.get(Calendar.MONTH) + 1;
        int curDay = calendar.get(Calendar.DAY_OF_MONTH);
        int age = curYear - year;
        if (!(curMonth > month || (curMonth == month && curDay > day))) {
            age--;
        }
        return age;
    }

    public static void writeLuckyNumber() {
        FileUtil.writeXMLLuckyNumber("conf/config.xml", luckNum);
    }

    private static void readWishText() {
        if (record.exists()) {
            wishText = FileUtil.readBinaryFile(record);
        } else if (backupRecord.exists()) {
            wishText = FileUtil.readBinaryFile(backupRecord);
        } else {
            wishText = "";
        }
    }

    public static void writeWishText() {
        FileUtil.createFile(record);
        FileUtil.createFile(backupRecord);
        FileUtil.writeBinaryFile(record, wishText);
        FileUtil.writeBinaryFile(backupRecord, wishText);
    }
}
